﻿using AutoMapper;
using BW.Demo.Domain.Entities;
using BW.Demo.ErrorCode;
using BW.Demo.Infrastructure.Interfcaes;
using BW.Demo.WebApi.Applications.Commands;
using MediatR;

namespace BW.Demo.WebApi.Applications.CommandHandlers
{
    public class TraceabilityDeleteCommandHandler : IRequestHandler<TraceabilityDeleteCommand, APIResult<int>>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRespitory<TraceabilityManagement> _baseRespitory;
        public TraceabilityDeleteCommandHandler(IMapper mapper, IBaseRespitory<TraceabilityManagement> baseRespitory)
        {
            _mapper = mapper;
            _baseRespitory = baseRespitory;
        }
        public async Task<APIResult<int>> Handle(TraceabilityDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            TraceabilityManagement trace = _mapper.Map<TraceabilityManagement>(request);
            trace.IsDeleted = true;
            int num = await _baseRespitory.UpdateAsync(trace);
            
            if (num > 0)
            {
                result.Code = ResultCode.删除成功;
                result.Message = ResultCode.删除成功.ToString();
                result.Data = num;

            }
            else
            {

                result.Code = ResultCode.删除失败;
                result.Message = ResultCode.删除失败.ToString();
                result.Data = num;
            }

            return await Task.FromResult(result);
        }
    }
}
